﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.IRepository.Gas;
using ZFine.Repository.Gas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data.SqlClient;

namespace ZFine.Application.Gas
{
    public class ApplyApp
    {
        private IApplyRepository service = new ApplyRepository();

        /// <summary>
        /// 客户端插入
        /// </summary>
        /// <param name="applyEntity"></param>
        public void ClientInsert(ApplyEntity applyEntity)
        {
            applyEntity.ClientCreate();
            service.Insert(applyEntity);
        }
        public List<ApplyEntity> GetList(Pagination pagination, string account)
        {
            var expression = ExtLinq.True<ApplyEntity>();
            if(!string.IsNullOrEmpty(account))
            {
                expression = expression.And(t => t.Phone == account);
            }
            return service.FindList(expression, pagination);
        }
        public List<ApplyEntity> GetListBySchool(Pagination pagination, string schoolId, string RName, string IDNumber, string OStatus, string HouseFlag,string HouseClass,string classValue)
        {
            var expression = ExtLinq.True<ApplyEntity>();
            if (!string.IsNullOrEmpty(schoolId))
            {
                expression = expression.And(t => t.SchoolId == schoolId);
            }
            if (!string.IsNullOrEmpty(RName))
            {
                expression = expression.And(t => t.StudentName.Contains(RName));
            }
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber == IDNumber);
            }
            if (!string.IsNullOrEmpty(OStatus))
            {
                expression = expression.And(t => t.OStatus == OStatus);
            }
            else
            {
                expression = expression.And(t => t.OStatus != "1");
            }
            if (!string.IsNullOrEmpty(HouseFlag) && "0" != HouseFlag)
            {
                expression = expression.And(t => t.HouseFlag == HouseFlag);
            }
            if (!string.IsNullOrEmpty(HouseClass))
            {
                expression = expression.And(t => t.HouseClass == HouseClass);
            }
            if (!string.IsNullOrEmpty(classValue))
            {
                expression = expression.And(t => t.Class == classValue);
            }
            
            return service.FindList(expression, pagination).ToList();
        }
        public List<ApplyEntity> GetListByEdu(Pagination pagination, string schoolId, string RName, string IDNumber, string OStatus, string txtHouseFlag, string admit,string HouseClass, string IsPwdKeyApply,string classValue)
        {
            var expression = ExtLinq.True<ApplyEntity>();
            if (!string.IsNullOrEmpty(schoolId))
            {
                expression = expression.And(t => t.SchoolId == schoolId);
            }
            if (!string.IsNullOrEmpty(RName))
            {
                expression = expression.And(t => t.StudentName.Contains(RName));
            }
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber == IDNumber);
            }
            if("1" != admit)
            {
                if (!string.IsNullOrEmpty(OStatus))
                {
                    expression = expression.And(t => t.OStatus == OStatus);
                }
            }
            else
            {
                expression = expression.And(t => t.OStatus == "4");//只选择预录取数据
            }
            if (!string.IsNullOrEmpty(txtHouseFlag) && "0" != txtHouseFlag)
            {
                expression = expression.And(t => t.HouseFlag == txtHouseFlag);
            }
            if (!string.IsNullOrEmpty(HouseClass))
            {
                expression = expression.And(t => t.HouseClass == HouseClass);
            }
            if (!string.IsNullOrEmpty(IsPwdKeyApply))
            {
                expression = expression.And(t => t.isPwdKeyApply == IsPwdKeyApply);
            }
            if (!string.IsNullOrEmpty(classValue))
            {
                expression = expression.And(t => t.Class == classValue);
            }
            return service.FindList(expression, pagination).ToList();
        }
        /// <summary>
        /// 获得IQueryable
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public IQueryable<ApplyEntity> GetIQueryable(string account)
        {
            var expression = ExtLinq.True<ApplyEntity>();
            if (!string.IsNullOrEmpty(account))
            {
                expression = expression.And(t => t.Phone == account);
            }
            return service.IQueryable(expression);
        }
        public int GetCount(string SchoolId, string classValue)
        {
            var expression = ExtLinq.True<ApplyEntity>();
            if (!string.IsNullOrEmpty(SchoolId))
            {
                expression = expression.And(t => t.SchoolId == SchoolId);
            }
            if (!string.IsNullOrEmpty(classValue))
            {
                expression = expression.And(t => t.Class == classValue);
            }
            expression = expression.And(t => (t.OStatus == "4" || t.OStatus == "5"));
            return service.IQueryable(expression).Count();
        }
        public List<ApplyEntity> GetApplyNumberList(string schoolId, string classValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT d.*
                              FROM SC_Apply d
                             WHERE 1=1
                               AND d.Class = @classValue
                               AND d.SchoolId = @schoolId
                               AND d.LastUpdateTime is not null
                               ORDER BY d.LastUpdateTime");
            DbParameter[] parameter ={
                    new SqlParameter("@classValue",classValue),
                    new SqlParameter("@schoolId",schoolId),
                };
            return service.FindList(strSql.ToString(), parameter);
        }
        public ApplyEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        //提交
        public void SubmitForm(ApplyEntity organizeEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                organizeEntity.Modify(keyValue);
                service.Update(organizeEntity);
            }
            else
            {
                organizeEntity.Create();
                service.Insert(organizeEntity);
            }
        }
        //删除
        public void Del(string keyValue)
        {
            service.Delete(a=>a.F_Id==keyValue);
        }
        //更新
        public void Update(ApplyEntity organizeEntity)
        {
            service.Update(organizeEntity);
        }
        //以身份证号检索数据
        public ApplyEntity GetFormByIDNumber(string txtIDNumber)
        {
            var expression = ExtLinq.True<ApplyEntity>();
            return service.FindEntity(expression.And(t=>t.IDNumber == txtIDNumber));
        }

        public List<ApplyEntity> GetListByEduForReport(string schoolId, string RName, string IDNumber, string OStatus, string txtHouseFlag, string admit, string HouseClass, string IsPwdKeyApply, string classValue)
        {
            var expression = ExtLinq.True<ApplyEntity>();
            if (!string.IsNullOrEmpty(schoolId))
            {
                expression = expression.And(t => t.SchoolId == schoolId);
            }
            if (!string.IsNullOrEmpty(RName))
            {
                expression = expression.And(t => t.StudentName.Contains(RName));
            }
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber == IDNumber);
            }
            if ("1" != admit)
            {
                if (!string.IsNullOrEmpty(OStatus))
                {
                    expression = expression.And(t => t.OStatus == OStatus);
                }
            }
            else
            {
                expression = expression.And(t => t.OStatus == "4");//只选择预录取数据
            }
            if (!string.IsNullOrEmpty(txtHouseFlag) && "0" != txtHouseFlag)
            {
                expression = expression.And(t => t.HouseFlag == txtHouseFlag);
            }
            if (!string.IsNullOrEmpty(HouseClass))
            {
                expression = expression.And(t => t.HouseClass == HouseClass);
            }
            if (!string.IsNullOrEmpty(IsPwdKeyApply))
            {
                expression = expression.And(t => t.isPwdKeyApply == IsPwdKeyApply);
            }
            if (!string.IsNullOrEmpty(classValue))
            {
                expression = expression.And(t => t.Class == classValue);
            }
            return service.IQueryable(expression).ToList();
        }
    }
}
